package cn.org.eshow.service.impl;

import cn.org.eshow.bean.query.InfoQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.InfoDao;
import cn.org.eshow.model.Blog;
import cn.org.eshow.model.Info;
import cn.org.eshow.model.User;
import cn.org.eshow.service.InfoManager;
import cn.org.eshow.service.UserManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebService;
import java.util.List;

@WebService(serviceName = "InfoService", endpointInterface = "cn.org.eshow.service.InfoManager")
@Service
public class InfoManagerImpl extends GenericManagerImpl<Info, Integer> implements InfoManager {

	@Autowired
	InfoDao infoDao;
	@Autowired
	UserManager userManager;

	@Autowired
	public InfoManagerImpl(InfoDao infoDao) {
		super(infoDao);
		this.infoDao = infoDao;
	}

    @Override
	public List<Info> list(InfoQuery query) {
		return infoDao.list(query);
	}

    @Override
	public Page<Info> search(InfoQuery query) {
		return infoDao.search(query);
	}

    @Override
	public Info browse(InfoQuery query) {
		List<Info> infos = infoDao.list(query);
		if (!infos.isEmpty()) {
			return infos.get(0);
		}
		return new Info();
	}

	@Override
	public void activityImg(User user) {
		Info info=browse(new InfoQuery("activityImg",true));
		if(info!=null && StringUtils.isNotEmpty(info.getImg())){
			user.setWebsite(info.getImg());
			userManager.save(user);
		}

	}

	@Override
	public Info updateInfo(Info old, Info info) {
		old.setType(StringUtils.isNotEmpty(info.getType())?info.getType():old.getType());
		old.setTitle(StringUtils.isNotEmpty(info.getTitle())?info.getTitle():old.getTitle());
		old.setContent(StringUtils.isNotEmpty(info.getContent())?info.getContent():old.getContent());
		old.setImg(StringUtils.isNotEmpty(info.getImg())?info.getImg():old.getImg());
		old.setUrl(StringUtils.isNotEmpty(info.getUrl())?info.getUrl():old.getUrl());
		old.setRemark(StringUtils.isNotEmpty(info.getRemark())?info.getRemark():old.getRemark());

		return infoDao.save(old);
	}

}